﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QuickGraph;

namespace EpidemicSimulator.states
{
    public class InfectiousState: State
    {
        public override void stateAction(long simTime)
        {
            Random random = new Random();
            
            //            if (node.socialNetwork.AdjacentEdges(node).ToList().Count() <= maxContacsPerStep)
            //            {
            //                foreach (Edge<SocialNetworkNode> edge in node.socialNetwork.AdjacentEdges(node))
            //                {
            //                    edge.Target.infect(simTime);
            //                }
            //            }
            //            else
            //            {
            //                int[] contacts = new int[maxContacsPerStep];
            //                for (int i = 0; i < maxContacsPerStep; i++) contacts[i] = -1;
            //                for (int i = 0; i < maxContacsPerStep; i++)
            //                {
            //                    int newContactNr;
            //                    do
            //                    {
            //                        newContactNr = random.Next(node.socialNetwork.AdjacentEdges(node).ToList().Count());
            //                    }
            //                    while(contacts.Contains(newContactNr));
            //                    contacts[i] = newContactNr;
            //                    if (node.socialNetwork.AdjacentEdges(node).ToArray()[newContactNr].Source.Equals(node))
            //                    {
            //                        node.socialNetwork.AdjacentEdges(node).ToArray()[newContactNr].Target.infect(simTime);
            //                    }
            //                    else
            //                    {
            //                        node.socialNetwork.AdjacentEdges(node).ToArray()[newContactNr].Source.infect(simTime);
            //                    }
            //                }
            //            }
            
            int maxRndContacts = random.Next(minContacsPerStep, maxContacsPerStep);
            //Console.WriteLine(maxRndContacts);
            int[] contacts = new int[maxRndContacts];
            for (int i = 0; i < maxRndContacts; i++) contacts[i] = -1;
            for (int i = 0; i < maxRndContacts; i++)
            {
                int newContactNr;
                do
                {
                    newContactNr = random.Next(node.socialNetwork.AdjacentEdges(node).ToList().Count());
                }
                while (contacts.Contains(newContactNr));
                contacts[i] = newContactNr;
                if (node.socialNetwork.AdjacentEdges(node).ToArray()[newContactNr].Source.Equals(node))
                {
                    node.socialNetwork.AdjacentEdges(node).ToArray()[newContactNr].Target.infect(simTime);
                }
                else
                {
                    node.socialNetwork.AdjacentEdges(node).ToArray()[newContactNr].Source.infect(simTime);
                }
            }

            double r = random.NextDouble();
            double p = 0;
            foreach (DiseaseModelEdge edge in diseaseModelGraph.OutEdges(stateNode))
            {
                p += edge.transitionProbability;
                if (r <= p && !changingState )
                {
                    State newState = getStateInstance(edge.Target.stateClass);
                    newState.diseaseModelGraph = diseaseModelGraph;
                    newState.stateNode = edge.Target;
                    newState.node = node;
                    newState.maxContacsPerStep = maxContacsPerStep;
                    newState.minContacsPerStep = minContacsPerStep;
                    int delay = random.Next(edge.minTransitionTime, edge.maxTransitionTime);
                    changeState(newState, simTime, delay);
                    return;
                }
            }
        }

        public override void infect(SocialNetworkNode infectingNode, long simTime)
        {
            
        }
    }
}
